package ctype

import (
	"bp/internal/util"
	"database/sql/driver"
	"fmt"
	"strings"
)

type OffGradientRule struct {
	Price      int32 `json:"price"`
	Quota      int32 `json:"quota"`
	OffMaximum int32 `json:"off_maximum"`
}

type OffGradientRuleArray []OffGradientRule

// Scan implements the Scanner interface.
func (r *OffGradientRule) Scan(value interface{}) error {
	// fmt.Printf("ic=>%v,%s", value, string(value.([]byte)))
	str := string(value.([]byte))
	val := str[1 : len(str)-1]
	vals := strings.Split(val, ",")
	r.Price = util.StringToInt32(vals[0])
	r.Quota = util.StringToInt32(vals[1])
	r.OffMaximum = util.StringToInt32(vals[2])
	return nil
}

// Value implements the driver Valuer interface.
func (r OffGradientRule) Value() (driver.Value, error) {
	return fmt.Sprintf("(%d,%d,%d)", r.Price, r.Quota, r.OffMaximum), nil
}
